package cn.iocoder.yudao.module.picturebook.service.workmember;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

import cn.iocoder.yudao.module.picturebook.controller.admin.workmember.vo.*;
import cn.iocoder.yudao.module.picturebook.dal.dataobject.workmember.WorkMemberDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.picturebook.dal.mysql.workmember.WorkMemberMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.picturebook.enums.ErrorCodeConstants.*;

/**
 * 作业与成员关系表	 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class WorkMemberServiceImpl implements WorkMemberService {

    @Resource
    private WorkMemberMapper workMemberMapper;

    @Override
    public Long createWorkMember(WorkMemberSaveReqVO createReqVO) {
        // 插入
        WorkMemberDO workMember = BeanUtils.toBean(createReqVO, WorkMemberDO.class);
        workMemberMapper.insert(workMember);

        // 返回
        return workMember.getMemberWorkId();
    }

    @Override
    public void updateWorkMember(WorkMemberSaveReqVO updateReqVO) {
        // 校验存在
        validateWorkMemberExists(updateReqVO.getMemberWorkId());
        // 更新
        WorkMemberDO updateObj = BeanUtils.toBean(updateReqVO, WorkMemberDO.class);
        workMemberMapper.updateById(updateObj);
    }

    @Override
    public void deleteWorkMember(Long id) {
        // 校验存在
        validateWorkMemberExists(id);
        // 删除
        workMemberMapper.deleteById(id);
    }

    @Override
    public void deleteWorkMemberListByIds(List<Long> ids) {
        // 删除
        workMemberMapper.deleteByIds(ids);
    }


    private void validateWorkMemberExists(Long id) {
        if (workMemberMapper.selectById(id) == null) {
            throw exception(WORK_MEMBER_NOT_EXISTS);
        }
    }

    @Override
    public WorkMemberDO getWorkMember(Long id) {
        return workMemberMapper.selectById(id);
    }

    @Override
    public PageResult<WorkMemberDO> getWorkMemberPage(WorkMemberPageReqVO pageReqVO) {
        return workMemberMapper.selectPage(pageReqVO);
    }

}